package com.winning.ywypt.controller;

import com.alibaba.fastjson.JSON;
import com.winning.ywypt.Entity.Fact_zwsj_mx_qysj_rb;
import com.winning.ywypt.Entity.Fact_zwsj_mx_qysj_rb_log;
import com.winning.ywypt.service.DataUploadService;
import com.winning.ywypt.service.GetDataService;
import com.winning.ywypt.service.impl.DataUploadServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: Datacontroller
 * @Author: sujun@winning.com.cn
 * @Desc: TODO
 * @Date: 2018-12-20 16:08
 **/

@RestController
public class Datacontroller {
    private final Logger logger = LoggerFactory.getLogger(DataUploadServiceImpl.class);

    @Autowired
    DataUploadService dataUploadService;

    @Autowired
    GetDataService getDataService;

    @Value("${com.winning.ywypt.appid}")
    private String appid;


    @RequestMapping("/UploadData")
    public void UploadData() {
        try {
            List<Fact_zwsj_mx_qysj_rb> zbxx = getDataService.getZbxx();
            if (zbxx.size() == 0) {
                logger.info("没有新数据...");
                return;
            }
//        获取token
            String token = dataUploadService.getToken();
            logger.info("获取到的Token值====>" + token);

            for (Fact_zwsj_mx_qysj_rb data : zbxx) {
                String head = " {\"data_schema\": {" +
                        "\"appid\": \"" + appid + "\"," +
                        "\"pk\": \"" + data.getLsh() + "\"," +
                        "\"updatetime\": \"" + data.getXt_xgrq() + "\"," +
                        "\"unit\": \"单位名称\"" +
                        "  }, \"data\":";
                String zbxxjson = JSON.toJSONString(data);

                dataUploadService.upLoadData(token, head + zbxxjson + "}");
//            记录日志
                getDataService.insertLog(new Fact_zwsj_mx_qysj_rb_log(data));
            }
//          更新修改标志位
            getDataService.updateFlag();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
